package com.jinwei.qunguanjia.web.group.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.utils.SystemContextUtils;
import com.jinwei.qunguanjia.user.dto.GroupCommentReplyDTO;
import com.jinwei.qunguanjia.user.dto.SensitiveWordDTO;
import com.jinwei.qunguanjia.user.service.GroupCommentReplyService;
import com.jinwei.qunguanjia.user.service.SensitiveWordService;
import com.jinwei.qunguanjia.web.group.vo.GroupCommentReplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 群评论回复管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/groupCommentReply")
public class GroupCommentReplyController {

    @Resource
    private GroupCommentReplyService groupCommentReplyService;

    @Resource
    private SensitiveWordService sensitiveWordService;

    /**
     * 回复接口
     * @param groupCommentReplyVO 对象
     */
    @PostMapping("/save")
    public Response save(@Validated @RequestBody GroupCommentReplyVO groupCommentReplyVO) throws IllegalAccessException {
        List<SensitiveWordDTO> sensitiveWordList = sensitiveWordService.list(new SensitiveWordDTO());
        for (SensitiveWordDTO sensitiveWord : sensitiveWordList) {
            if (groupCommentReplyVO.getComment().indexOf(sensitiveWord.getSensitiveWordContent()) > -1) {
                groupCommentReplyVO.setComment(groupCommentReplyVO.getComment().replaceAll(sensitiveWord.getSensitiveWordContent(), "*"));
            }
        }
        GroupCommentReplyDTO groupCommentReplyDTO = new GroupCommentReplyDTO();
        BeanUtils.copyProperties(groupCommentReplyVO, groupCommentReplyDTO);
        groupCommentReplyDTO.setUserId(SystemContextUtils.getUserId());
        groupCommentReplyService.save(groupCommentReplyDTO);
        return Response.success();
    }

}
